import { Node } from "../singleLinkList";


/**
 * 获取链表的中心节点
 * @param head
 */
export const getCenterLinkedList = <T>(head: Node<T>) => {
    if (head === null || head.next === null) return head;
    // 快慢指针
    let slow: Node<T> | null = head,
        fast: Node<T> | null = head;
    
    // 快指针继续走的条件
    while(fast !== null && fast.next !== null && fast.next.next !== null) {
        // 满指针走一步，满指针走两步
        slow = slow!.next;
        fast = fast.next.next;
    }
    
    return slow;
}